package com.manager.common.job;

import com.manager.common.ssh.RemoteExecuteLinuxCommandTools;
import com.manager.common.ssh.StringUtils;
import com.manager.common.tcp.client.TcpClient;
import com.manager.common.tcp.constant.TcpDataType;
import com.manager.common.tcp.decode.impl.BusinessDecode;
import com.manager.common.utils.DataTools;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import javax.xml.crypto.Data;
import java.util.ArrayList;

/**
 * @ClassName DisposeReportCommandThread
 * @Description TODO
 * @Author @LiuHongJun
 * @Date 2025-10-10 9:53
 * @Version 1.0
 */
public class DisposeReportCommandThread  extends Thread{


    private String ip;

    private Integer port;

    private String password;

    private String userName;

    public DisposeReportCommandThread(String ip, Integer port, String password, String userName) {
        this.ip = ip;
        this.port = port;
        this.password = password;
        this.userName = userName;
    }

    @Override
    public void run() {
        while (true){
            try {
                BusinessDecode businessDecode = TcpClient.getInstance().getDisposeReportCommandQueue().take();
                if(businessDecode.getContent().equals(TcpDataType.CONTAINER_MANAGER)){
                    handleContainerManager(businessDecode);
                }
            }catch (Exception e){

            }
        }
    }

    /***
     {"type":6,"serialNumber":"1755505231",
     "containers":[{"containerId":1,"code":1,
     "command":"docker run -d --name=mimic-container1 --cap-add=NET_ADMIN --cap-add=SYS_PTRACE --restart=unless-stopped --network=pf1-macvlan
     --ip=192.168.11.100 -e CONTAINER_ID=1 -e ERROR_INJECT_CONTAINERS=1 -e ERROR_INJECT_TYPE=modify_byte -e ERROR_INJECT_POSITION=5 mimic-container"}]}
     */
    private void handleContainerManager(BusinessDecode businessDecode){
        //1：创建，2 ：启动，3：停止，4：重启，5：删除
        JSONObject jsonObject = JSONObject.fromObject(businessDecode.getContent());
        JSONArray jsonArray = jsonObject.getJSONArray("containers");
        for(int i = 0;i < jsonArray.size();i++){
            JSONObject tempJson = jsonArray.getJSONObject(i);
            int code = tempJson.getInt("code");
            String command = "";
            if(code == 1){
                command = tempJson.getString("command");
            }else if(code == 2){
                command = " docker start " + DataTools.containerPrefix + tempJson.getString("containerId");
            }else if(code == 3){
                command = " docker stop " + DataTools.containerPrefix + tempJson.getString("containerId");
            }else if(code == 4){
                command = " docker restart " + DataTools.containerPrefix + tempJson.getString("containerId");
            }else if(code == 5){
                command = " docker rm " + DataTools.containerPrefix + tempJson.getString("containerId");
            }
            if(StringUtils.isEmpty(command)){
                continue;
            }
            RemoteExecuteLinuxCommandTools.excuteRemoteSSH(ip,userName,password,port,new ArrayList<>(),command);
        }
    }
}
